package com.zhuiyun.project.api.system.businesDept.controller;

import com.zhuiyun.project.annotation.OptLog;
import com.zhuiyun.project.api.system.businesDept.entity.SysDept;
import com.zhuiyun.project.api.system.businesDept.service.SysBusinesDeptService;
import com.zhuiyun.project.common.constant.UserConstants;
import com.zhuiyun.project.common.controller.BaseController;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.StringUtils;
import com.zhuiyun.project.utils.constants.CommonConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName SysBusinesDeptConteoller
 * @Description TODO 租户部门管理
 * @Author wx
 * @Date 2023/12/5 20:55
 **/
@RestController
@Api(tags = "租户部门管理")
@Slf4j
@CrossOrigin
@RequestMapping("/SysBusinesDept")
public class SysBusinesDeptConteoller extends BaseController {
    @Autowired
    private SysBusinesDeptService sysBusinesDeptService;

    /**
    * @Author wx
    * @Description 获取租户部门列表
    * @Date 2023/12/6 8:51
    * @Return
    * @param dept
    */
    @GetMapping("/list")
    @ApiOperation(value = "获取部门列表")
    @OptLog(modular = "部门管理", type = CommonConstants.SELECT, describe = "查询部门列表")
    public CommonResult list(SysDept dept)
    {
        List<SysDept> depts = sysBusinesDeptService.selectDeptList(dept);
        return CommonResult.ok(depts);
    }

    /**
    * @Author wx
    * @Description 新增部门信息
    * @Date 2023/12/6 9:51
    * @Return
    * @param dept
    */
    @ApiOperation(value = "新增部门信息")
    @OptLog(modular = "部门管理", type = CommonConstants.SELECT, describe = "add")
    @PostMapping("/adddept")
    public CommonResult  adddept(@RequestBody SysDept dept){
        System.out.println(dept);
        if (!sysBusinesDeptService.checkDeptNameUnique(dept))
        {
            return CommonResult.error("新增部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        dept.setCreateBy(getUsername());
        return CommonResult.ok(sysBusinesDeptService.insertDept(dept));
    }

    /**
    * @Author wx
    * @Description 根据id查询部门管理的数据
    * @Date 2023/12/6 10:45
    * @Return
    * @param dept
    */
    @ApiOperation(value = "根据id查询部门管理的数据")
    @OptLog(modular = "部门管理", type = CommonConstants.SELECT, describe = "select")
    @PutMapping("/selectByDeptId")
    public CommonResult getInfo(@RequestBody SysDept dept)
    {
        sysBusinesDeptService.checkDeptDataScope(dept.getDeptId(),dept.getVenueUniqueIdentifier());
        return CommonResult.ok(sysBusinesDeptService.selectDeptById(dept.getDeptId(),dept.getVenueUniqueIdentifier()));
    }

    /**
    * @Author wx
    * @Description 查询部门列表
    * @Date 2023/12/6 10:50
    * @Return
    * @param dept
    */
    @ApiOperation(value = "查询部门列表")
    @OptLog(modular = "部门管理", type = CommonConstants.SELECT, describe = "select")
    @PutMapping("/exclude")
    public CommonResult exclude(@RequestBody SysDept dept){
        Long deptId = dept.getDeptId();
        SysDept dept1 = new SysDept();
        dept1.setVenueUniqueIdentifier(dept.getVenueUniqueIdentifier());
        List<SysDept> depts = sysBusinesDeptService.selectDeptList(dept1);
        depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
        return CommonResult.ok(depts);
    }

    /**
    * @Author wx
    * @Description 修改部门列表
    * @Date 2023/12/6 11:40
    * @Return
    * @param dept
    */
    @ApiOperation(value = "修改部门列表")
    @OptLog(modular = "部门管理", type = CommonConstants.UPDATE, describe = "修改部门列表")
    @PutMapping("/updateDept")
    public CommonResult edit(@RequestBody SysDept dept)
    {
        Long deptId = dept.getDeptId();
        String venueUniqueIdentifier = dept.getVenueUniqueIdentifier();
        sysBusinesDeptService.checkDeptDataScope(deptId,venueUniqueIdentifier);
        if (!sysBusinesDeptService.checkDeptNameUnique(dept))
        {
            return CommonResult.error("修改部门'" + dept.getDeptName() + "'失败，部门名称已存在");
        }
        else if (dept.getParentId().equals(deptId))
        {
            return CommonResult.error("修改部门'" + dept.getDeptName() + "'失败，上级部门不能是自己");
        }
        else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && sysBusinesDeptService.selectNormalChildrenDeptById(deptId,venueUniqueIdentifier) > 0)
        {
            return CommonResult.error("该部门包含未停用的子部门！");
        }
        dept.setUpdateBy(getUsername());
        return CommonResult.ok(sysBusinesDeptService.updateDept(dept));
    }

    /**
    * @Author wx
    * @Description 删除部门列表
    * @Date 2023/12/6 14:08
    * @Return
    * @param dept
    */
    @ApiOperation(value = "删除部门列表")
    @OptLog(modular = "部门管理", type = CommonConstants.UPDATE, describe = "删除部门列表")
    @PutMapping("/delDept")
    public CommonResult remove(@RequestBody SysDept dept) {
        System.out.println(dept);
        Long deptId = dept.getDeptId();
        String venueUniqueIdentifier = dept.getVenueUniqueIdentifier();
        if (sysBusinesDeptService.hasChildByDeptId(deptId,venueUniqueIdentifier))
        {
            return CommonResult.error("存在下级部门,不允许删除");
        }
        if (sysBusinesDeptService.checkDeptExistUser(deptId,venueUniqueIdentifier))
        {
            return CommonResult.error("部门存在用户,不允许删除");
        }
        sysBusinesDeptService.checkDeptDataScope(deptId,venueUniqueIdentifier);
        return CommonResult.ok(sysBusinesDeptService.deleteDeptById(deptId,venueUniqueIdentifier));
    }
}
